﻿using System.Activities.Presentation;
using System.IO;
using Headsight.BPMN2WF.BPMN.Model;
using Headsight.BPMN2WF.BPMN.Schemas;
using Headsight.BPMN2WF.Testing.TestObjCtors;
using Headsight.BPMN2WF.Transformation.BPMN2TransformerMappings.Config;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Headsight.BPMN2WF
{
    [TestClass]
    public class BPMN2WFConversionTest
    {
        private readonly string _mappingFile = Properties.Settings.Default.TestingRootDir + @"\Headsight.BPMN2WFTest\Transformation\MappingConfigFiles\MappingAdvanced.xml";
        private readonly string _destinationFilename = Properties.Settings.Default.TestingRootDir + @"\Headsight.BPMN2WFTest.Workflows\Models\BPMN2WFConversionTest.xaml";
        
        public WorkflowDesigner Convert(bool simplify, bool beautify)
        {
            var sut = BPMN2WFFactory.Create(new BPMN2WFTransformationConfig(_mappingFile), simplify, null);

            var bpmnModel = CreateBPMNModel(new tProcessCtor()
                .WithStartEvent("start")
                .Flow("start", "gateway_id")
                .Branch("gateway_id", "branch_1_id")
                .Branch("gateway_id", "branch_2_id"));

            var result = sut.Transform(bpmnModel);
            return (WorkflowDesigner)result.Model;
        }

        private static BPMNFlowNodesBlock CreateBPMNModel(ITestObjCtor<tProcess> tProcessCtor)
        {
            var tDefinitions = new tDefinitionsCtor().With(tProcessCtor.Create()).Create();

            return new BPMNFlowNodesBlockFactory()
                .CreateBPMNFlowNodesBlock(tDefinitions);
        }

        private void CompareStringToFile(string expected)
        {
            string s;
            using (var rdr = File.OpenText(_destinationFilename))
            {
                s = rdr.ReadToEnd();
            }
            Assert.AreEqual(expected, s);
        }

        [TestMethod]
        public void TestNoSimplifyNoBeautify()
        {
            var wfDesigner = Convert(false, false);

            wfDesigner.Flush();
            var expected = wfDesigner.Text;

            wfDesigner.Save(_destinationFilename);

            CompareStringToFile(expected);
        }

        [TestMethod]
        public void TestSimplifyNoBeautify()
        {
            var wfDesigner = Convert(true, false);

            wfDesigner.Flush();
            var expected = wfDesigner.Text;

            wfDesigner.Save(_destinationFilename);

            CompareStringToFile(expected);
        }
    }
}
